"use strict";
var g_func_hiddenClass = null;
GameUI.SetCameraTerrainAdjustmentEnabled( false );

function localHiddenMsg(){
	$('#errorMsgBG').SetHasClass("AniScaleOut",false);
	$('#errorMsgBG').SetHasClass("Hidden",true);
}
function localShowMsg(){
	$('#errorMsgBG').SetHasClass("AniScaleOut",true);
	$('#errorMsgBG').SetHasClass("Hidden",false);
}

function onErrorMsg(data){
	var text = data.text
	var errorMsgBG = $('#errorMsgBG');

	if (errorMsgBG.BHasClass("AniScaleOut")){
		localHiddenMsg();
		$.CancelScheduled(g_func_hiddenClass);
	}
	localShowMsg();
	$('#errorMsgTxt').text = $.Localize(text);
	g_func_hiddenClass = $.Schedule(1.2,localHiddenMsg);
}

GameEvents.Subscribe( "S2C_ERROR_MSG", onErrorMsg );
localHiddenMsg();

var team = [0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var damage = [0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var teamnum = 0;
var playernum = 0;
var dict = {};

function checkPlayerAndTeam()
{
	var lastteam = -1;
	for (var i = 0;i<15;i++) {
		if(Players.IsValidPlayerID( i )){
			playernum++;
			var team = Players.GetTeam( i )
			if (dict[team]==undefined){
				dict[team] = 0;
				teamnum++;
			}else{
				dict[team]++;
			}
		}
	}
}
checkPlayerAndTeam();

function updateRank(){
	var lastteam = -1;
	for(var i=0;i<playernum;i++){
		
		var preteam = Players.GetTeam( i-1 );
		var curteam = Players.GetTeam( i );
		if (i==0){
			preteam = Players.GetTeam( i );
		} 
		if(curteam != preteam ){
			//插入一条总积分
			var teamData = CustomNetTables.GetTableValue( "teamInfo", preteam.toString());
			if($('#team'+preteam)==undefined){
				//没有这行，添加一行
				var panel = $.CreatePanel("Panel", $('#rankPanel'), "team"+preteam);
	    		panel.BLoadLayoutSnippet("teamInfo");
	    		var txt = panel.FindChildTraverse("rank");
	    		txt.text = Math.floor(teamData["damage"]);
			}else{
				//已经存在这一项就直接修改
				var txt = $('#team'+preteam).FindChildTraverse("rank");
				txt.text = Math.floor(teamData["damage"]);
			}
		}
		lastteam = curteam;
		//加入新的一行
		var primData = CustomNetTables.GetTableValue( "primInfo", i.toString());
		var steamData = CustomNetTables.GetTableValue("steamId",i.toString());
		if($('#player'+i)==undefined){
			//没有这行，添加一行
			var panel = $.CreatePanel("Panel", $('#rankPanel'), "player"+i);
    		panel.BLoadLayoutSnippet("rankInfo");
    		var txt = panel.FindChildTraverse("rank");
    		txt.text = Math.floor(primData["damage"]);
    		var avatar = panel.FindChildTraverse("avatar");
    		var name = panel.FindChildTraverse("name");
    		avatar.steamid = steamData["steamid"];
    		name.steamid = steamData["steamid"];
		}else{
			//已经存在这一项就直接修改
			var txt = $('#player'+i).FindChildTraverse("rank");
			txt.text = Math.floor(primData["damage"]);
		}
	}
	//最后加入最后一个队伍的总积分
	var teamData = CustomNetTables.GetTableValue( "teamInfo", lastteam.toString());

	if($('#team'+lastteam)==undefined){
		//没有这行，添加一行
		var panel = $.CreatePanel("Panel", $('#rankPanel'), "team"+lastteam);
		panel.BLoadLayoutSnippet("teamInfo");
		var txt = panel.FindChildTraverse("rank");
		txt.text = Math.floor(teamData["damage"]);
	}else{
		//已经存在这一项就直接修改
		var txt = $('#team'+lastteam).FindChildTraverse("rank");
		txt.text = Math.floor(teamData["damage"]);
	}

	$.Schedule( 1, updateRank );
}
updateRank();

function animateScale(panel){
	if (panel.BHasClass("hideQuest")) {
		panel.AddClass("showQuest");
		panel.RemoveClass("hideQuest");
		panel.style.visibility = 'visible';
		//$('#QuestRoot').AddClass("RootHeight");

	}else{
		panel.RemoveClass("showQuest");
		panel.AddClass("hideQuest");
		$.Schedule(0.2,function () {
			//$('#QuestRoot').RemoveClass("RootHeight");
			panel.style.visibility = 'collapse';
		});
	}
}

function OnShowRank(){
	animateScale($('#rankPanel'));
}

function initRankPanel(){
	$('#rankPanel').RemoveAndDeleteChildren();
	OnShowRank();
}
initRankPanel();
Game.AddCommand( "shortcut_score_panel", OnShowRank, "", 0 );